package com.sinog.front.controller.basic;

import com.sinog.core.model.UserDTO;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import com.sinog.front.config.log.Log;
import com.sinog.front.feign.basic.ResourceService;
import com.sinog.front.service.user.UserFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;

/**
 * @desc 系统管理 - 资源管理 - 资源管理 - ResourceController
 * @author zy
 * @date 2019年6月28日 15点51分
 */
@RestController
public class ResourceController {

    @Autowired
    private ResourceService resourceService;
    @Autowired
    private UserFacade userFacade;

    /**
     * 系统管理 - 资源管理 - 资源管理 - 保存资源信息信息
     * @param request request
     * @param resource resource
     * @return Result
     */
    @Log(type = "更新", value = "保存资源信息信息 参数中有id为【更新】 没有id为【新增】")
    @PostMapping("/basic/resource")
    public Result saveResource(HttpServletRequest request,String resource) {
        UserDTO userDTO = userFacade.getAuthentication();
        if(Constants.RoleId.ROLE_ID_AB.equals(userDTO.getRoleid())) {
            return resourceService.save(resource);
        }
        return Result.failure(ReturnCode.NO_PERMISSION);
    }

    /**
     * 系统管理 - 资源管理 - 资源管理 - 批量删除资源信息信息
     * @param request request
     * @param ids ids
     * @return Result
     */
    @Log(type = "删除", value = "删除资源信息信息")
    @DeleteMapping("/basic/resource")
    public Result deleteResource(HttpServletRequest request,@RequestParam("ids[]") String[] ids) {
        UserDTO userDTO = userFacade.getAuthentication();
        if(Constants.RoleId.ROLE_ID_AB.equals(userDTO.getRoleid())) {
            return resourceService.deleteBatch(ids);
        }
        return Result.failure(ReturnCode.NO_PERMISSION);
    }

    /**
     * 系统管理 - 资源管理 - 资源管理 - 根据ID查询单个资源信息
     * @param id id
     * @return Result
     */
    @GetMapping("/basic/resource/{id}")
    public Result selectResource(@PathVariable String id) {
        UserDTO userDTO = userFacade.getAuthentication();
        if(Constants.RoleId.ROLE_ID_AB.equals(userDTO.getRoleid())) {
            return resourceService.selectOne(id);
        }
        return Result.failure(ReturnCode.NO_PERMISSION);
    }

    /**
     * 系统管理 - 资源管理 - 资源管理 - 复制自己的资源到某一个资源菜单下
     * @param request request
     * @param src src
     * @param dest dest
     * @return Result
     */
    @Log(type = "新增", value = "复制资源信息")
    @PostMapping("/basic/resource/copy")
    public Result copyResources(HttpServletRequest request,Long src,Long dest) {
        UserDTO userDTO = userFacade.getAuthentication();
        if(Constants.RoleId.ROLE_ID_AB.equals(userDTO.getRoleid())) {
            return resourceService.copyResources(src,dest);
        }
        return Result.failure(ReturnCode.NO_PERMISSION);
    }

    /**
     * 系统管理 - 资源管理 - 资源管理 - 获取资源类型
     * @return String
     */
    @GetMapping("/basic/resource/type")
    public String selectType() {
        UserDTO userDTO = userFacade.getAuthentication();
        if(Constants.RoleId.ROLE_ID_AB.equals(userDTO.getRoleid())) {
            Result result = resourceService.selectResourceType();
            String json = "";
            if(Constants.Number.NUM_ONE.equals(result.getCode())) {
                json = JsonUtil.toText(result.getData());
            }
            return json;
        }
        return ReturnCode.NO_PERMISSION.message();
    }

    /**
     * 系统管理 - 资源管理 - 资源管理 - 加载资源管理模块的资源树
     * @param reskey reskey
     * @return String
     */
    @GetMapping("/basic/resources/{reskey}")
    public String selectResourceAsTree(@PathVariable String reskey) {
        UserDTO userDTO = userFacade.getAuthentication();
        if(Constants.RoleId.ROLE_ID_AB.equals(userDTO.getRoleid())) {
            Result result = resourceService.selectResourceAsTree(reskey);
            String json = "";
            if(Constants.Number.NUM_ONE.equals(result.getCode())) {
                json = JsonUtil.toText(result.getData());
            }
            return json;
        }
        return ReturnCode.NO_PERMISSION.message();
    }

    /**
     * 系统管理 - 资源管理 - 资源管理 - 加载首页导航栏菜单
     * @return String
     */
    @GetMapping("/basic/resource/menu")
    public String selectResourceMenu() {
        UserDTO userDTO = userFacade.getAuthentication();
        Result result = resourceService.selectResouceMenu(userDTO.getAccount());
        String json = "";
        if(Constants.Number.NUM_ONE.equals(result.getCode())) {
            json = JsonUtil.toText(result.getData());
        }
        return json;
    }
}